const mysql = require('mysql');
const path = require('path')
const dotenvAbsolutePath = path.join(__dirname, '../.env')
require('dotenv').config({
    path: dotenvAbsolutePath,
});

const pool = mysql.createPool({
    host: process.env.MYSQL_HOST,
    user: process.env.MYSQL_USER,
    password: process.env.MYSQL_PASSWORD,
    database: process.env.MYSQL_DATABASE, // 数据库
    port: process.env.MYSQL_PORT,
    multipleStatements:true,
    dateStrings:true
});
function query(sql){
    return new Promise(function (resolve, reject) {
        pool.getConnection(function (err, connection) {
            if(err){
                reject(err);
            }else {
                connection.query(sql, function (err, rows) {
                    if(err){
                        reject(err);
                    }else {
                        resolve(rows);
                    }
                    connection.release();
                })
            }
        })
    })
}

function close(){
    pool.end();
}

exports.query = query;
exports.close = close;